package com.ls.fw.data.result;

import java.sql.SQLException;
import java.util.List;

import com.ls.fw.data.bean.Column;
import com.ls.fw.data.bean.Record;


/**
 * 
 * @author ls
 * 2014年11月25日上午9:51:41
 */
public class DataResultSetMetaDataImpl extends DataResultSetMetaDataAdpater{

	private DataResult dataResult;
	
	
	public DataResultSetMetaDataImpl(DataResult dataResult) {
		super();
		this.dataResult = dataResult;
	}

	@Override
	public int getColumnCount() throws SQLException {
		List<Record> list = dataResult.getResults();
		int count = 0;
		if(list.size()>0){
			Record r = list.get(0);
			count = r.getFields().size();
		}
		return count;
	}

	@Override
	public String getColumnName(int column) throws SQLException {
		String name = null;
		List<Record> list = dataResult.getResults();
		if(list.size()>0){
			Record r = list.get(0);
			List<Column> lists = r.getFields();
			if(lists.size() >= column && column >= 1){
				Column f = lists.get(column-1);
				name = f.getShowName();
			}
		}
		return name;
	}

	private Column getCloumn(final int column){
		Column f = null;
		List<Record> list = dataResult.getResults();
		if(list.size()>0){
			Record r = list.get(0);
			List<Column> lists = r.getFields();
			if(lists.size() >= column && column >= 1){
				f = lists.get(column-1);
			}
		}
		return f;
	}
	
	@Override
	public String getSchemaName(int column) throws SQLException {
		Column f = this.getCloumn(column);
		String name = null;
		if(f != null){
			name = (f.getTable()!=null?f.getTable().getSchemaName():null);
		}
		return name;
	}

	@Override
	public String getTableName(int column) throws SQLException {
		Column f = this.getCloumn(column);
		String name = null;
		if(f != null){
			name = (f.getTable()!=null?f.getTable().getName():null);
		}
		return name;
	}
	
}
